package com.guli.gulimall.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 朱财艺
 * @date : 2021/12/10
 * @Description:
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProductDeam {

    @Autowired
    private CategoryDao categoryDao;

    @Test
    public void category(){

        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
        w1.eq("parent_cid",0);
        List<CategoryEntity> categoryEntities = categoryDao.selectList(w1);
        for (CategoryEntity l1 : categoryEntities) {
            Long catId = l1.getCatId();
            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
            w2.eq("parent_cid",catId);
            List<CategoryEntity> categoryEntities1 = categoryDao.selectList(w2);
            l1.setChildren(categoryEntities1);
            for (CategoryEntity categoryEntity : categoryEntities1) {
                Long catId1 = categoryEntity.getCatId();
                QueryWrapper<CategoryEntity> w3 = new QueryWrapper<>();
                w3.eq("parent_cid",catId1);
                List<CategoryEntity> categoryEntities2 = categoryDao.selectList(w3);
                categoryEntity.setChildren(categoryEntities2);
            }

        }

        System.out.println(categoryEntities);
    }
     @Test
    public void tree(){
         List<CategoryEntity> entityList = categoryDao.selectList(null);
         List<CategoryEntity> collect = entityList.stream().filter(s -> {
             return s.getParentCid().equals(0L);
         }).collect(Collectors.toList());

         List<CategoryEntity> collect1 = collect.stream().map(s -> {

             List<CategoryEntity> chlie = chlie(entityList, s);
             s.setChildren(chlie);
             return s;
         }).collect(Collectors.toList());
         System.out.println(collect1);
     }

     public List<CategoryEntity> chlie(List<CategoryEntity> categoryEntities,CategoryEntity categoryEntity){

         List<CategoryEntity> collect = categoryEntities.stream().filter(s -> {
             return s.getParentCid().equals(categoryEntity.getCatId());
         }).collect(Collectors.toList());

         List<CategoryEntity> collect1 = collect.stream().map(s -> {
             List<CategoryEntity> chlie = chlie(categoryEntities, s);
             s.setChildren(chlie);
             return s;
         }).collect(Collectors.toList());
         return collect1;
     }

    }
